<?php

	include_once ( "AbstractDAO.php" );

	class ShopCategoryDAO extends AbstractDAO
	{
		
		//Get Level 1 shop Category from table product_category
		function GetLevel1Category()
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "topLevelShopCate", CNS_INDEX );
			if( $re !== false )
				return $re;
				
			$result = $this->db->GetPlan( "select id, cate_name from shop_category where parent_id=0" );

			$shopCateList = array();
			foreach ( $result as $item )
			{
				$shopCateList[$item['cate_name']]=$item['id'];
		    }
		    $cacheManager->Set( "topLevelShopCate", $shopCateList, CNS_INDEX );
		    return $shopCateList;
		}
		
		function GetShopCategoryName($scid)
		{
			if($scid=='0')
				return '';//取不到显示为空
			else
				return $this->db->GetValue("select cate_name from shop_category where id=".$scid);
		}
		
		/**
		 * Convert taobao-cid to Local Cid
		 */
		function ConvertTaobaoShopCid($taobao_cid)
		{
		
			$result=$this->db->GetRow("select id, parent_id from shop_category where taobao_cid=".$taobao_cid);
			
			if($result){
				
			   return $result['id']; 
				
			}else
			   return '0';//找不到返回0
	
		}

		/**
		 * return category names by category id list
		 * @param array $ids
		 * @return array
		 */
		function GetShopCateInfoByIdList( $ids )
		{
			if(count($ids)==0)
				return false;
			else
			{
				$strCateIds = implode( ",", $ids );
				//$result = $this->db->GetPlan( "select id, cate_name from shop_category where id in (".$strCateIds.") ORDER BY FIELD(id, ".$strCateIds.")" );
				$result = $this->db->GetPlan( "select id, cate_name from shop_category where id in (".$strCateIds.")" );
				return $this->SortByIdArray( $result, $ids );
			}
		}
	} 